import 'dart:convert';
import 'package:crypto/crypto.dart';
import 'package:dio/dio.dart';

class BaseInterceptor extends QueuedInterceptorsWrapper {
  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
    try {
      const apiSecret = String.fromEnvironment("apiSecret");
      String verifyTime = DateTime.now().millisecondsSinceEpoch.toString();
      var bytes = utf8.encode("$apiSecret$verifyTime");
      String verifyEncrypt = md5.convert(bytes.toList()).toString();
      // options.headers['verify-time'] = verifyTime;
      // options.headers['verify-encrypt'] = verifyEncrypt;
      options.headers['Authorization'] =
          "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3NDMzMTQ1NjcsImlzcyI6IjA5MjhEMjI3MDc2RDRGQzBCM0FENTkwRkI3RTRFNjNEIiwibmJmIjoxNzM2ODM0NTYyfQ.AMTtBar3AV-UhiidvzqkNDlSPoc7KtCNZy3zQi0liGg";
      // options.headers['Businessid'] = 1;
      // if (app.loginResEntity != null) {
      //   options.headers['token'] = app.loginResEntity?.token;
      // }
      if (options.method.toLowerCase() == "post") {
        /// 添加公共参数
        var data = (options.data ?? <String, dynamic>{}) as Map<String, dynamic>;
        // if (app.configInfoEntity != null) {
        //   data.addAll(app.configInfoEntity!.toCommonPamras());
        // }

        // String jsonString = json.encode(data);
        // final mdtString = JsEncryptUtils.encodeMd5('$timeStamp${jsonString}Mimo-vivi$randomString');
        // options.headers['x-sign'] = mdtString;
        //
        // var encodeString = JsEncryptUtils.aesEncrypt(jsonString);
        // options.data = encodeString;
      }
      handler.next(options);
    } catch (e) {
      // logger?.e(e);
      super.onRequest(options, handler);
    }
  }

  @override
  void onResponse(Response response, ResponseInterceptorHandler handler) {
    try {
      if (response.statusCode == 200) {
        // var data = response.data as String;
        // var decodeData = JsEncryptUtils.aesDecrypt(data);
        // var mapData = jsonDecode(decodeData);
        // response.data = mapData;
        handler.next(response);
      } else {
        super.onResponse(response, handler);
      }
    } catch (e) {
      super.onResponse(response, handler);
    }
  }

  @override
  void onError(DioException err, ErrorInterceptorHandler handler) {
    handler.reject(err);
  }

  /// [object]  解析的对象
  /// [deep]  递归的深度，用来获取缩进的空白长度
  /// [isObject] 用来区分当前map或list是不是来自某个字段，则不用显示缩进。单纯的map或list需要添加缩进
  String _convert(dynamic object, int deep, {bool isObject = false}) {
    var buffer = StringBuffer();
    var nextDeep = deep + 1;
    if (object is Map) {
      var list = object.keys.toList();
      if (!isObject) {
        //如果map来自某个字段，则不需要显示缩进
        buffer.write("${getDeepSpace(deep)}");
      }
      buffer.write("{");
      if (list.isEmpty) {
        //当map为空，直接返回‘}’
        buffer.write("}");
      } else {
        buffer.write("\n");
        for (int i = 0; i < list.length; i++) {
          buffer.write("${getDeepSpace(nextDeep)}\"${list[i]}\":");
          buffer.write(_convert(object[list[i]], nextDeep, isObject: true));
          if (i < list.length - 1) {
            buffer.write(",");
            buffer.write("\n");
          }
        }
        buffer.write("\n");
        buffer.write("${getDeepSpace(deep)}}");
      }
    } else if (object is List) {
      if (!isObject) {
        //如果list来自某个字段，则不需要显示缩进
        buffer.write("${getDeepSpace(deep)}");
      }
      buffer.write("[");
      if (object.isEmpty) {
        //当list为空，直接返回‘]’
        buffer.write("]");
      } else {
        buffer.write("\n");
        for (int i = 0; i < object.length; i++) {
          buffer.write(_convert(object[i], nextDeep));
          if (i < object.length - 1) {
            buffer.write(",");
            buffer.write("\n");
          }
        }
        buffer.write("\n");
        buffer.write("${getDeepSpace(deep)}]");
      }
    } else if (object is String) {
      //为字符串时，需要添加双引号并返回当前内容
      buffer.write("\"$object\"");
    } else if (object is num || object is bool) {
      //为数字或者布尔值时，返回当前内容
      buffer.write(object);
    } else {
      //如果对象为空，则返回null字符串
      buffer.write("null");
    }
    return buffer.toString();
  }

  ///获取缩进空白符
  String getDeepSpace(int deep) {
    var tab = StringBuffer();
    for (int i = 0; i < deep; i++) {
      tab.write("\t");
    }
    return tab.toString();
  }
}
